/*
 * Copyright (c) 2007, Dennis N. Mehay
 * ProbSign.java
 *
 * Created on November 4, 2007, 5:58 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package opennlp.ccg.synsem;

import java.util.List;
import opennlp.ccg.grammar.Rule;
import opennlp.ccg.grammar.TypeChangingRule;
import opennlp.ccg.hylo.HyloHelper;
import opennlp.ccg.lexicon.Word;
import opennlp.ccg.parse.DerivationHistory;
import opennlp.ccg.util.SingletonList;

/**
 *
 * @author Dennis N. Mehay
 */
public class ProbSign extends Sign {
    
    /** Creates a new instance of ProbSign */
    public ProbSign() {
    }
        
    /** Constructor with derivation history. */
    @SuppressWarnings("unchecked")
	protected ProbSign(List<Word> words, Category cat, DerivationHistory dh, double prob) {
        super(words, cat, dh);
        super.logProb = prob;
    }

    /** Constructor with no additional derivation history. */
    public ProbSign(List<Word> words, Category cat, double prob) {
        this(words, cat, null, prob);
        super._history = new DerivationHistory(this);
    }

    /** Constructor with no additional derivation history. */
    public ProbSign(Word word, Category cat, double prob) {        
        this(new SingletonList<Word>(word), cat, prob);
    }
        
    /** Factory method for creating derived signs with the given cat from the given inputs and rule. */
    public static ProbSign createDerivedSign(Category cat, Sign[] inputs, Rule rule, double prob) {
        return new ProbSign(cat, inputs, rule, prob);
    }

    /** Factory method for creating derived signs from the given result cat, inputs and rule, 
        with a new LF constructed from the inputs.
        Note that unlike with rule applications, the result LF is constructed with 
        no var substitutions, so it is useful only for creating alternative signs during realization. */
    public static ProbSign createDerivedSignWithNewLF(Category cat, Sign[] inputs, Rule rule, double prob) {
        Category copyCat = cat.shallowCopy();
        LF lf = null;
        for (int i = 0; i < inputs.length; i++) {
            lf = HyloHelper.append(lf, inputs[i].getCategory().getLF());
        }
        if (rule instanceof TypeChangingRule) {
            TypeChangingRule tcr = (TypeChangingRule) rule;
            lf = HyloHelper.append(lf, tcr.getResult().getLF());
        }
        if (lf != null) { HyloHelper.sort(lf); }
        copyCat.setLF(lf);
        Category resultCat = internCat(copyCat);
        return new ProbSign(resultCat, inputs, rule, prob);
    }
        
    /** Constructor with words and derivation history formed from the given inputs and rule. */
    protected ProbSign(Category cat, Sign[] inputs, Rule rule, double prob) {
        this(getRemainingWords(inputs, 0), cat, null, prob);
        _history = new DerivationHistory(inputs, this, rule);
    }
}
